home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS02.ADF / IFF / iffw.c < prev    next >
Text File  |  1989-05-30  |  8KB  |  216 lines

  1. /*----------------------------------------------------------------------*
  2.  * IFFW.C  Support routines for writing IFF-85 files.          11/15/85
  3.  * (IFF is Interchange Format File.)
  4.  *
  5.  * By Jerry Morrison and Steve Shaw, Electronic Arts.
  6.  * This software is in the public domain.
  7.  *
  8.  * This version for the Commodore-Amiga computer.
  9.  *----------------------------------------------------------------------*/
  10. #include "iff/iff.h"
  11.  
  12. /* ---------- IFF Writer -----------------------------------------------*/
  13.  
  14. /* A macro to test if a chunk size is definite, i.e. not szNotYetKnown.*/
  15. #define Known(size)   ( (size) != szNotYetKnown )
  16.  
  17. /* Yet another weird macro to make the source code simpler...*/
  18. #define IfIffp(expr)  {if (iffp == IFF_OKAY)  iffp = (expr);}
  19.  
  20.  
  21. /* ---------- OpenWIFF -------------------------------------------------*/
  22. IFFP OpenWIFF(file, new0, limit)  BPTR file; GroupContext *new0; LONG limit; {
  23.     register GroupContext *new = new0;
  24.     register IFFP iffp = IFF_OKAY;
  25.  
  26.     new->parent       = NULL;
  27.     new->clientFrame  = NULL;
  28.     new->file         = file;
  29.     new->position     = 0;
  30.     new->bound        = limit;
  31.     new->ckHdr.ckID   = NULL_CHUNK;  /* indicates no current chunk */
  32.     new->ckHdr.ckSize = new->bytesSoFar = 0;
  33.  
  34.     if (0 > Seek(file, 0, OFFSET_BEGINNING))    /* Go to start of the file.*/
  35.     iffp = DOS_ERROR;
  36.     else if ( Known(limit) && IS_ODD(limit) )
  37.     iffp = CLIENT_ERROR;
  38.     return(iffp);
  39.     }
  40.  
  41. /* ---------- StartWGroup ----------------------------------------------*/
  42. IFFP StartWGroup(parent, groupType, groupSize, subtype, new)
  43.       GroupContext *parent, *new; ID groupType, subtype; LONG groupSize;  {
  44.     register IFFP iffp;
  45.  
  46.     iffp = PutCkHdr(parent, groupType, groupSize);
  47.     IfIffp( IFFWriteBytes(parent, (BYTE *)&subtype, sizeof(ID)) );
  48.     IfIffp( OpenWGroup(parent, new) );
  49.     return(iffp);
  50.     }
  51.  
  52. /* ---------- OpenWGroup -----------------------------------------------*/
  53. IFFP OpenWGroup(parent0, new0)  GroupContext *parent0, *new0; {
  54.     register GroupContext *parent = parent0;
  55.     register GroupContext *new    = new0;
  56.     register LONG ckEnd;
  57.     register IFFP iffp = IFF_OKAY;
  58.  
  59.     new->parent       = parent;
  60.     new->clientFrame  = parent->clientFrame;
  61.     new->file         = parent->file;
  62.     new->position     = parent->position;
  63.     new->bound        = parent->bound;
  64.     new->ckHdr.ckID   = NULL_CHUNK;
  65.     new->ckHdr.ckSize = new->bytesSoFar = 0;
  66.  
  67.     if ( Known(parent->ckHdr.ckSize) ) {
  68.     ckEnd = new->position + ChunkMoreBytes(parent);
  69.     if ( new->bound == szNotYetKnown || new->bound > ckEnd )
  70.         new->bound = ckEnd;
  71.     };
  72.  
  73.     if ( parent->ckHdr.ckID == NULL_CHUNK || /* not currently writing a chunk*/
  74.      IS_ODD(new->position) ||
  75.          (Known(new->bound) && IS_ODD(new->bound)) )
  76.     iffp = CLIENT_ERROR;
  77.     return(iffp);
  78.     }
  79.  
  80. /* ---------- CloseWGroup ----------------------------------------------*/
  81. IFFP CloseWGroup(old0)  GroupContext *old0; {
  82.     register GroupContext *old = old0;
  83.  
  84.     if ( old->ckHdr.ckID != NULL_CHUNK )  /* didn't close the last chunk */
  85.     return(CLIENT_ERROR);
  86.     if ( old->parent == NULL ) {      /* top level file context */
  87.     /* [TBD] set logical EOF */
  88.     }
  89.     else {
  90.     old->parent->bytesSoFar += old->position - old->parent->position;
  91.     old->parent->position = old->position;
  92.     };
  93.     return(IFF_OKAY);
  94.     }
  95.  
  96. /* ---------- EndWGroup ------------------------------------------------*/
  97. IFFP EndWGroup(old)  GroupContext *old;  {
  98.     register GroupContext *parent = old->parent;
  99.     register IFFP iffp;
  100.  
  101.     iffp = CloseWGroup(old);
  102.     IfIffp( PutCkEnd(parent) );
  103.     return(iffp);
  104.     }
  105.  
  106. /* ---------- PutCk ----------------------------------------------------*/
  107. IFFP PutCk(context, ckID, ckSize, data)
  108.       GroupContext *context; ID ckID; LONG ckSize; BYTE *data; {
  109.     register IFFP iffp = IFF_OKAY;
  110.  
  111.     if ( ckSize == szNotYetKnown )
  112.     iffp = CLIENT_ERROR;
  113.     IfIffp( PutCkHdr(context, ckID, ckSize) );
  114.     IfIffp( IFFWriteBytes(context, data, ckSize) );
  115.     IfIffp( PutCkEnd(context) );
  116.     return(iffp);
  117.     }
  118.  
  119. /* ---------- PutCkHdr -------------------------------------------------*/
  120. IFFP PutCkHdr(context0, ckID, ckSize)
  121.       GroupContext *context0;  ID ckID;  LONG ckSize; {
  122.     register GroupContext *context = context0;
  123.     LONG minPSize = sizeof(ChunkHeader); /* physical chunk >= minPSize bytes*/
  124.  
  125.     /* CLIENT_ERROR if we're already inside a chunk or asked to write
  126.      * other than one FORM, LIST, or CAT at the top level of a file */
  127.     /* Also, non-positive ID values are illegal and used for error codes.*/
  128.     /* (We could check for other illegal IDs...)*/
  129.     if ( context->ckHdr.ckID != NULL_CHUNK  ||  ckID <= 0 )
  130.     return(CLIENT_ERROR);
  131.     else if (context->parent == NULL)  {
  132.     switch (ckID)  {
  133.         case FORM:  case LIST:  case CAT:  break;
  134.         default: return(CLIENT_ERROR);
  135.         }
  136.     if (context->position != 0)
  137.         return(CLIENT_ERROR);
  138.     }
  139.  
  140.     if ( Known(ckSize) ) {
  141.     if ( ckSize < 0 )
  142.         return(CLIENT_ERROR);
  143.     minPSize += ckSize;
  144.     };
  145.     if ( Known(context->bound)  &&
  146.          context->position + minPSize > context->bound )
  147.     return(CLIENT_ERROR);
  148.  
  149.     context->ckHdr.ckID   = ckID;
  150.     context->ckHdr.ckSize = ckSize;
  151.     context->bytesSoFar   = 0;
  152.     if (0 > Write(context->file, &context->ckHdr, sizeof(ChunkHeader)))
  153.     return(DOS_ERROR);
  154.     context->position += sizeof(ChunkHeader);
  155.     return(IFF_OKAY);
  156.     }
  157.  
  158. /* ---------- IFFWriteBytes ---------------------------------------------*/
  159. IFFP IFFWriteBytes(context0, data, nBytes)
  160.       GroupContext *context0;  BYTE *data;  LONG nBytes; {
  161.     register GroupContext *context = context0;
  162.  
  163.     if ( context->ckHdr.ckID == NULL_CHUNK  ||    /* not in a chunk */
  164.      nBytes < 0  ||                /* negative nBytes */
  165.      (Known(context->bound)  &&        /* overflow context */
  166.         context->position + nBytes > context->bound)  ||
  167.      (Known(context->ckHdr.ckSize)  &&       /* overflow chunk */
  168.         context->bytesSoFar + nBytes > context->ckHdr.ckSize) )
  169.     return(CLIENT_ERROR);
  170.  
  171.     if (0 > Write(context->file, data, nBytes))
  172.     return(DOS_ERROR);
  173.  
  174.     context->bytesSoFar += nBytes;
  175.     context->position   += nBytes;
  176.     return(IFF_OKAY);
  177.     }
  178.  
  179. /* ---------- PutCkEnd -------------------------------------------------*/
  180. IFFP PutCkEnd(context0)  GroupContext *context0; {
  181.     register GroupContext *context = context0;
  182.     WORD zero = 0;    /* padding source */
  183.  
  184.     if ( context->ckHdr.ckID == NULL_CHUNK )  /* not in a chunk */
  185.     return(CLIENT_ERROR);
  186.  
  187.     if ( context->ckHdr.ckSize == szNotYetKnown ) {
  188.     /* go back and set the chunk size to bytesSoFar */
  189.     if ( 0 > Seek(context->file,
  190.               -(context->bytesSoFar + sizeof(LONG)),
  191.               OFFSET_CURRENT)  ||
  192.          0 > Write(context->file, &context->bytesSoFar, sizeof(LONG))  ||
  193.          0 > Seek(context->file, context->bytesSoFar, OFFSET_CURRENT)  )
  194.         return(DOS_ERROR);
  195.     }
  196.     else {  /* make sure the client wrote as many bytes as planned */
  197.     if ( context->ckHdr.ckSize != context->bytesSoFar )
  198.         return(CLIENT_ERROR);
  199.     };
  200.  
  201.     /* Write a pad byte if needed to bring us up to an even boundary.
  202.      * Since the context end must be even, and since we haven't
  203.      * overwritten the context, if we're on an odd position there must
  204.      * be room for a pad byte. */
  205.     if ( IS_ODD(context->bytesSoFar) ) {
  206.     if ( 0 > Write(context->file, &zero, 1) )
  207.         return(DOS_ERROR);
  208.     context->position += 1;
  209.     };
  210.  
  211.     context->ckHdr.ckID   = NULL_CHUNK;
  212.     context->ckHdr.ckSize = context->bytesSoFar = 0;
  213.     return(IFF_OKAY);
  214.     }
  215.  
  216.